package edu.chalmers.trec;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import edu.chalmers.targine.Arbject;
import edu.chalmers.targine.TextBillboard;

public class PlanetArbject extends Arbject {
	public Planet planet;

	private float timer = 0;

	private TextBillboard title;
	private TextBillboard extra;

	private static int quadTexture = -1;
	private static int redTex = -1;
	private static int greenTex = -1;
	private static int blueTex = -1;

	public PlanetArbject(String name, String patternID, String nfcID,
			String owner, Context context) {
		super(name, patternID, nfcID, context);
		//
		planet = new Planet("unknown", 0, 0, 0, 0);

		title = new TextBillboard(mContext, "", 12.5f, -20f, -1f, 30f, 180f);
		extra = new TextBillboard(mContext, "", 12.5f, 0f, -2f, 30f, 180f);
		addTextBillboard(title);
		addTextBillboard(extra);
	}

	@Override
	public void markForReset() {
		super.markForReset();
	}

	public void setPlanet(Planet planet) {
		this.planet = planet;
	}

	public void addFood(int amount) {
		planet.food += amount;
		extra.setText(planet.toString());
		markForReinitialization();
	}

	public void addOxygen(int amount) {
		planet.oxygen += amount;
		extra.setText(planet.toString());
		markForReinitialization();
	}

	public void addEnergy(int amount) {
		planet.energy += amount;
		extra.setText(planet.toString());
		markForReinitialization();
	}

	public void addPopulation(int amount) {
		planet.population += amount;
		extra.setText(planet.toString());
		markForReinitialization();
	}

	public void updateForPack(String pack) {
		planet.updateForPack(pack);
	}

	public void update(float dt) {
		timer += dt;
		if (timer > 1) {
			planet.update(dt);
			markForReinitialization();
			timer = 0;
		}
	}

	@Override
	public void init(GL10 gl) {
		// load card textures
		if (quadTexture == -1) {
			quadTexture = loadTexture(gl, mContext, R.drawable.card_bd);
			redTex = loadTexture(gl, mContext, R.drawable.icon_red);
			greenTex = loadTexture(gl, mContext, R.drawable.icon_green);
			blueTex = loadTexture(gl, mContext, R.drawable.icon_blue);
		}

		title.setText(planet.name);
		extra.setText(planet.toString());
		addBillboard(quadTexture, 0f, 0f, 0f, 60f, 90f);

		// if(card != null){
		// if(card.type.equals(Card.RESOURCES)){
		// title.setText("Resources");
		// extra.setText("money:" + money + ", vp:" + victoryPoints);
		//
		// addBillboard(quadTexture, 0f, 0f, 0f, 60f, 90f);
		// } else{
		// title.setText(card.name);
		// extra.setText("" + card.power);
		//
		// addBillboard(quadTexture, 0f, 0f, 0f, 60f, 90f);
		// if(hasColor(Card.RED)){
		// addBillboard(redTex, -25f, 10f, -1f, 5f, 90f);
		// } if(hasColor(Card.GREEN)){
		// addBillboard(greenTex, -25f, 20f, -1f, 5f, 90f);
		// } if(hasColor(Card.BLUE)){
		// addBillboard(blueTex, -25f, 30f, -1f, 5f, 90f);
		// }
		// }
		// }

		super.init(gl);
	}

	@Override
	public void draw(GL10 gl) {
		super.draw(gl);
	}

	@Override
	public void clear() {
		super.clear();
		title.setText("");
		extra.setText("");
		addTextBillboard(title);
		addTextBillboard(extra);
	}
}